package cn.com.dealme.service.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import cn.com.dealme.DealMeRuntimeException;
import cn.com.dealme.domain.vo.UniversityVO;
import cn.com.dealme.service.UniversityService;
import cn.com.dealme.service.exception.UniversityException;

public class UniversityServiceImpl implements UniversityService {
	
	private Session session;

	@SuppressWarnings("unchecked")
	public List<UniversityVO> listAll() {
		return session.createCriteria(UniversityVO.class).list();
	}

	public UniversityVO get(Long id) {
		return (UniversityVO) session.get(UniversityVO.class, id);
	}

	public void update(UniversityVO universityVO) {
		get(universityVO.getId()).setName(universityVO.getName());
	}
	

	public void add(UniversityVO university) {
		if(exist(university)) {
			throw UniversityException.DoAddButExist;
		}
		session.save(university);
	}

	@SuppressWarnings("unchecked")
	public boolean exist(UniversityVO universityVO) {
		Criteria c = session.createCriteria(UniversityVO.class);
		c.add(Restrictions.eq("name", universityVO.getName()));
		List<UniversityVO> list = c.list();
		if(list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public void delete(Long [] idArr) {
		for(Long id : idArr) {
			session.delete(session.load(UniversityVO.class, id));
		}
	}



}
